﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BabyFood.Entities;
using BabyFood.Data;
using System.Drawing;

public partial class SachHay_CNSachHay : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        imgViewer.ImageUrl = H_AnhDaiDien.Value.Length > 0 ? Common.sPathUpSachHayThumbail + H_AnhDaiDien.Value : "~/Images/no_image.jpg";
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                H_CatalogId.Value = Request.QueryString["id"].ToString();
                BindData(Convert.ToInt32(H_CatalogId.Value));
            }
        }
    }

    public string Uploadedfilename { get { return Convert.ToString(ViewState["filename"]); } set { ViewState["filename"] = value; } }

    protected void BindData(int IdSachHay)
    {

        SachHay entSachHay = DataRepository.SachHayProvider.GetById(IdSachHay);
        txtTenQC.Text = entSachHay.TenSach;
        txtNXB.Text = entSachHay.TenNhaXuatBan;
        chkTinNong.Checked = entSachHay.TrangThai;
        txtTomTat.Text = entSachHay.TomTat;
        txtTrongLuong.Text = entSachHay.TrongLuong;
        txtKichThuoc.Text = entSachHay.KichThuoc;
        txtTheLoai.Text = entSachHay.TheLoai;
        txtLienKet.Text = entSachHay.LienKet;
        txtTacGia.Text = entSachHay.TacGia;
        txtSoTrang.Text = Convert.ToString(entSachHay.SoTrang);
        txtHinhThuc.Text = entSachHay.HinhThucBia;
        imgViewer.ImageUrl = entSachHay.AnhDaiDien.Length > 0 ? Common.sPathUpSachHayThumbail + "/" + entSachHay.AnhDaiDien : "~/Images/no_image.jpg";
        H_FileName.Value = entSachHay.AnhDaiDien;
        H_TepTin.Value = entSachHay.LienKet;
        txtContent.Text = Server.HtmlDecode(entSachHay.NoiDung);
        txtGiaBan.Text = entSachHay.GiaBan.ToString();
        txtNgayKetThuc.Text = entSachHay.NgayCapNhat.ToString("dd/MM/yyyy");

    }

    protected void cmdLuu_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {

            SachHay entSachHay = new SachHay();
            entSachHay.Id = Convert.ToInt32(H_CatalogId.Value);
            entSachHay.AnhDaiDien = H_FileName.Value;
            entSachHay.TenSach = Common.StandardString(txtTenQC.Text);
            entSachHay.TenNhaXuatBan = Common.StandardString(txtNXB.Text);
            entSachHay.NoiDung = Server.HtmlEncode(txtContent.Text);
            entSachHay.GiaBan = Convert.ToInt32(txtGiaBan.Text);
            entSachHay.TomTat = Common.StandardString(txtTomTat.Text);
            entSachHay.NgayCapNhat = DateTime.ParseExact(Common.StandardString(txtNgayKetThuc.Text), "dd/MM/yyyy", null);
            entSachHay.TomTat = txtTomTat.Text;
            entSachHay.LienKet = txtLienKet.Text;
            entSachHay.TheLoai = Common.StandardString(txtTheLoai.Text);
            entSachHay.TacGia = Common.StandardString(txtTacGia.Text);
            entSachHay.TrongLuong = Common.StandardString(txtTrongLuong.Text);
            entSachHay.KichThuoc = Common.StandardString(txtKichThuoc.Text);
            entSachHay.SoTrang = Convert.ToInt32(txtSoTrang.Text);
            entSachHay.HinhThucBia = Common.StandardString(txtHinhThuc.Text);
            //Upload pdf

            entSachHay.LienKet = Common.StandardString(H_TepTin.Value);

            if (H_AnhDaiDien.Value.Length == 0)
            {
                entSachHay.AnhDaiDien = "~/Images/no_image.jpg";
            }
            else
            {
                entSachHay.AnhDaiDien = Common.StandardString(H_AnhDaiDien.Value);
            }

            entSachHay.TrangThai = chkTinNong.Checked;
            if (entSachHay.Id == 0)
            {
                updateStatus = DataRepository.SachHayProvider.Insert(entSachHay);
                cmdNhapTiep_Click(sender, e);
            }
            else
            {

                entSachHay.EntityState = EntityState.Changed;
                updateStatus = DataRepository.SachHayProvider.Update(entSachHay);
                cmdNhapTiep_Click(sender, e);
            }

            if (!updateStatus)
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.SACHHAY_CAPNHAT_THATBAI, true);
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.SACHHAY_CAPNHAT_THANHCONG, true);
                H_CatalogId.Value = entSachHay.Id.ToString();
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("CNSachHay.aspx.cs", "cmdLuu_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.SACHHAY_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdNhapTiep_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        imgViewer.ImageUrl = "~/Images/no_image.jpg";
        H_AnhDaiDien.Value = "~/Images/no_image.jpg";
        txtTenQC.Text = "";
        txtNXB.Text = "";
        txtNgayKetThuc.Text = "";
        txtLienKet.Text = "";
        txtTrongLuong.Text = "";
        txtTacGia.Text = "";
        txtTheLoai.Text = "";
        txtKichThuoc.Text = "";
        txtSoTrang.Text = "0";
        txtHinhThuc.Text = "";
        txtContent.Text = "";
        txtTomTat.Text = "";
        chkTinNong.Checked = true;
        txtGiaBan.Text = "0";
    }

}